package com.example.Controller;

import com.alibaba.fastjson.JSONObject;
import com.example.Domain.Manual;
import com.example.Domain.User;
import com.example.Domain.Vodeo;
import com.example.Util.JwtUtil;
import com.example.common.ServerResponse;
import com.example.service.Impl.VodeoServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/vodeo")
public class VodeoController {
    @Autowired
    private VodeoServiceImpl vodeoService;
    @RequestMapping("/vodeList")
    public ServerResponse vodeList(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            JSONObject jsonObject = JSONObject.parseObject(data);
            System.out.println(jsonObject+":jsonObject");
            System.out.println(data+":data");
            Integer pageNum = jsonObject.getInteger("pageNum");
            Integer pageSize = jsonObject.getInteger("pageSize");
            PageHelper.startPage(pageNum, pageSize);
            List<Vodeo> a=vodeoService.vodeList(pageNum,pageSize);
            PageInfo<Vodeo> pageInfo = new PageInfo<>(a);
            System.out.println(pageInfo);

            return ServerResponse.createBySuccess("查询成功",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("查询失败");
        }
    }
    /*数据回显*/
    @RequestMapping("/echovodeo")
    public ServerResponse echovodeo(HttpServletRequest request,Integer id){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            List<Vodeo> a=vodeoService.echovodeo(id);
            return ServerResponse.createBySuccess("成功",a);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("失败");
        }

    }
}
